package com.codingfuture.glacierhardware.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codingfuture.glacierhardware.entity.WareHouse;
import com.codingfuture.glacierhardware.mapper.WareHouseMapper;
import com.codingfuture.glacierhardware.service.WareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Author: HanLe
 * @DateTime: 2025/4/1 14:29
 * @Description:
 **/
@Service
public class WareHouseServiceImpl implements WareHouseService {
    @Autowired
    private WareHouseMapper warehouseMapper;
    
    @Override
    public Page<WareHouse> selectByPage(String name, Page<WareHouse> page) {
        QueryWrapper<WareHouse> wrapper = new QueryWrapper<>();
        wrapper.like(!(name==null|| name.isEmpty()),"name", name);
        return warehouseMapper.selectPage(page,wrapper);
    }

    @Override
    public void add(WareHouse warehouse) {
        // 自动生成sn
        if (warehouse.getSn() == null || warehouse.getSn().isEmpty()) {
            warehouse.setSn("WH" + UUID.randomUUID().toString().replace("-", "").substring(0, 14));
        }
        warehouseMapper.insert(warehouse);
    }

    @Override
    public void updateById(Long id, WareHouse warehouse) {
        warehouse.setId(id);
        warehouseMapper.updateById(warehouse);
    }

    @Override
    public void deleteById(Long id) {
        warehouseMapper.deleteById(id);
    }

    @Override
    public WareHouse findById(Long id) {
        return warehouseMapper.selectById(id);
    }

    @Override
    public void updateLocation(Long id, Double longitude, Double latitude) {
        WareHouse warehouse = new WareHouse();
        warehouse.setId(id);
        warehouse.setLongitude(longitude);
        warehouse.setLatitude(latitude);
        warehouseMapper.updateById(warehouse);
    }
}
